<template>
	<view class="container">
		
		<view class="detailArea">
			<!-- 标题放在bar上面,传参也行,搜完之后设置也行 -->
			<view class="detailItem">
				<!-- 轮播图 -->
				<swiper indicator-dots style="min-height:30vh;">
					<swiper-item v-for="(item,index) in swipers" :key="index">
						<image mode="aspectFit" :src="item"></image>
					</swiper-item>
				</swiper>
		
				<text>类型:</text>
				<uni-card>
					<text>{{type}}</text>
				</uni-card>
				<text>时间:</text>
				<uni-card>
					<text>{{time}}</text>
				</uni-card>
				<text>地点:</text>
				<uni-card>
					<text>{{place}}</text>
				</uni-card>
				<text>详情:</text>
				<uni-card>
					<text>{{content}}</text>
				</uni-card>
				<!-- 阅读数-start -->
				<text class="top-read-before">加入日程数：</text>
				<span class="top-read">{{addedTimes}}</span>
			</view>
		
		</view>
		
		<view class="yes-btn">
			<button type="primary" @click="verify" :disabled="isChecked">审核通过</button>
		</view>
		<view class="no-btn">
			<button type="primary" @click="deleteActivity">删除活动</button>
		</view>
		
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :type="msgType" cancelText="关闭" confirmText="确认" title="提示信息" :content="messageText"></uni-popup-dialog>
		</uni-popup>
		
	</view>
</template>

<script>
	
	Date.prototype.Format = function (fmt) {
	    var o = {
	        "M+": this.getMonth() + 1, // 月份
	        "d+": this.getDate(), // 日
	        "h+": this.getHours(), // 小时
	        "m+": this.getMinutes(), // 分
	        "s+": this.getSeconds(), // 秒
	        "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
	        "S": this.getMilliseconds() // 毫秒
	    };
	    if (/(y+)/.test(fmt))
	        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	    for (var k in o)
	        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	            return fmt;
	}
	
	export default {
		data() {
			return {
				apiUrl:"http://localhost:8080/",
				id:0,
				title:"",
				isChecked:true,
				type: "",
				place: "",
				time:"",
				content: "",
				addedTimes: 0,
				swipers: [],
				msgType:"",
				messageText:""
			}
		},
		onLoad:function(option){
			console.log(option.id);
			console.log(option.title);
			console.log(option.state);
			this.id=option.id;
			this.title=option.title;
			uni.setNavigationBarTitle({
				title:option.title
			});
			if(option.state == "withdraw"){
				this.isChecked=false;
			};
			this.getActivityDetail();
		},
		methods: {
			getActivityDetail() {
				//获取活动信息
				uni.request({
					method: 'GET',
					url: this.apiUrl + 'activity/' + this.id,
			
					header: {
						Authorization: "Bearer "+uni.getStorageSync("token")
						// Authorization: "Bearer " +
						// 	"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJoenMiLCJleHAiOjE2NzA5NDQ1NzMsImlhdCI6MTY3MDkyNjU3M30.2xSkDjb5b8gE-NAd-YdxP3LgewgVo1H6ncx1QCcujTQu2V248kRrjygb544cWQmsuPpzH3jwbNPQqQwtAlFCeQ"
			
					},
					success: (res) => {
						console.log(res);
						let tempTime = res.data.beginTime;
						let tempPlace = res.data.place;
						if(res.data.beginTime.includes("，")){
							let btimes = res.data.beginTime.split("，");
							let etimes = res.data.endTime.split("，");
							let stages = res.data.stageNames.split("，");
							tempTime = "";
							for(let i = 0;i<btimes.length;i++){
								tempTime+="阶段 "+stages[i]+" 的开始时间是 "+btimes[i]+" 结束时间是 "+etimes[i]+"\n";
							}
						}
						else{
							tempTime = res.data.beginTime+"~"+res.data.endTime;
						}
						
						if(res.data.place.includes("，")){
							let places = res.data.place.split("，");
							let stages = res.data.stageNames.split("，");
							tempPlace = "";
							for(let i = 0;i<places.length;i++){
								tempPlace+="阶段 "+stages[i]+" 的举办地点是 "+places[i]+"\n";
							}
						}
						
						this.title = res.data.name;
						this.content = res.data.content;
						this.addedTimes = res.data.addedTimes;						
						this.time = tempTime;
						this.type = res.data.type;
						this.place = tempPlace;
						if (res.data.pic1) {
							this.swipers.push(res.data.pic1);
						}
						if (res.data.pic2) {
							this.swipers.push(res.data.pic2);
						}
						if (res.data.pic3) {
							this.swipers.push(res.data.pic3);
						}
						console.log(this.swipers);
					},
					fail: (err) => {
						return uni.showToast({
							icon: 'loading',
							title: '请求失败'
						})
						reject(err)
					}
				})
			},
			verify(){
				uni.request({
						method:'PUT',
						url:this.apiUrl+'activity/verify/'+this.id,
						header:{
							'Content-Type':'application/x-www-form-urlencoded',
							Authorization : uni.getStorageSync("token")
							},
						data:{
							'verify':true
						},
						success: (res) => {
							console.log(res.data);
							this.isChecked=true;
							this.addOperation("审核通过");
							// uni.navigateBack();
						},
						fail: (err) => {
										return uni.showToast({
											icon: 'loading',
											title:'请求失败'
										})
										reject(err)
									}
					})
			},
			deleteActivity(){
				uni.request({
						method:'DELETE',
						url:this.apiUrl+'activity/'+this.id,
						header:{
								Authorization : uni.getStorageSync("token")
							},
						success: (res) => {
							console.log(res);
							if(res.data.message!=null){
								this.msgType = "error";
								this.messageText = "删除失败!";
								this.$refs.alertDialog.open()
							}
							else{
								console.log("删除成功!");
								this.addOperation("删除活动");
							}
							// uni.navigateBack();
						},
						fail: (err) => {
										return uni.showToast({
											icon: 'loading',
											title:'请求失败'
										})
										reject(err)
									}
					})
			},
			addOperation(type){
				var time = new Date().Format("yyyy-MM-dd hh:mm:ss");
				console.log(time);
				uni.request({
						method:'POST',
						url:this.apiUrl+'operation',
						header:{
							Authorization : uni.getStorageSync("token")
							},
						data:{
							'time':time,
							'adminId':uni.getStorageSync("uid"),
							'type':type,
							'sourceId':this.id
						},
						success: (res) => {
							console.log(res.data);
							uni.navigateBack();
						},
						fail: (err) => {
										return uni.showToast({
											icon: 'loading',
											title:'请求失败'
										})
										reject(err)
									}
					})
			}
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
	.detailArea{
		padding-bottom: 50px;
		position: relative
	}

	.detailArea image{
		position: absolute;
		left:0;right:0;top:0;bottom:0;margin:auto
	}
	.top-read {
		font-size: 28rpx;
		padding-left: 10rpx;
		color: #999999;
	}
	
	.top-read-before {
		font-size: 28rpx;
	
	}
	
	.yes-btn button{
		position:fixed;
		bottom:1px;
		width:45%;
		margin: 0% 0%;
/* 		background: #2979ff;
		color: #fff; */
		background-image: linear-gradient(to top, #66b7f9,#1c82d4);
	}
	.no-btn button{
		position:fixed;
		bottom:1px;
		width:45%;
		margin: 0% 46%;
		/* background: #2979ff;
		color: #fff; */
		background-image: linear-gradient(to top, #66b7f9,#1c82d4);
	}
</style>
